package com.guigu.controller.design;


import com.guigu.pojo.design.DLevelFirst;
import com.guigu.pojo.design.DLevelSecond;
import com.guigu.pojo.stock.SSecondProductArea;
import com.guigu.service.design.DLevelFirstService;
import com.guigu.service.design.DLevelSecondService;
import com.guigu.service.stock.SSecondProductAreaService;
import com.guigu.utils.CodeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mr.Zhang
 * @since 2024-12-17
 */
@Controller
@RequestMapping("/d-level-second")
public class DLevelSecondController {

    @Autowired
    private DLevelFirstService dLevelFirstService;

    @Autowired
    private DLevelSecondService dLevelSecondService;

    @Autowired
    private SSecondProductAreaService sSecondProductAreaService;

    @RequestMapping("/initialLevel")
    @ResponseBody
    public ArrayList<DLevelSecond> initialLevel(HttpServletRequest req){
        // 一级id
        String firstId = StringUtils.isNotBlank(req.getParameter("firstId")) ? req.getParameter("firstId") : null;
        ArrayList<DLevelSecond> dLevelSeconds = dLevelSecondService.selectLevelSecondByFirstId(firstId);
        return dLevelSeconds;
    }

    @RequestMapping("/initialLevelII")
    @ResponseBody
    public ArrayList<DLevelSecond> initialLevelII(){
        ArrayList<DLevelSecond> dLevelSeconds = dLevelSecondService.selectLevelSecond();
        System.out.println(dLevelSeconds);
        return dLevelSeconds;
    }

    @GetMapping("/initSelect")
    @ResponseBody
    public ArrayList<DLevelFirst> initSelect(){
        ArrayList<DLevelFirst> dLevelFirsts = dLevelFirstService.selectAllLevelFirst();
        return dLevelFirsts;
    }

    @PostMapping("/insertSecond")
    @ResponseBody
    public Boolean insertSecond(@RequestBody DLevelSecond second){
        DLevelSecond dLevelSecond = dLevelSecondService.selectDLevelSecondOrderByDescOne();
        CodeUtils codeUtils = new CodeUtils();
        String newKindId = codeUtils.getNewKindId(dLevelSecond.getSecondId());
        second.setSecondId(newKindId);
        Boolean b = dLevelSecondService.insertSecond(second);

        SSecondProductArea sSecondProductArea = new SSecondProductArea();
        sSecondProductArea.setSecondId(second.getSecondId());
        sSecondProductArea.setSecondName(second.getSecondName());
        sSecondProductArea.setParentId(second.getParentId());
        Boolean b2 = sSecondProductAreaService.insertSSecondProductArea(sSecondProductArea);
        if(b && b2){
            return true;
        }
        return false;
    }
}

